package com.cgq.base.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.cgq.base.entity.AjaxResult;
import com.cgq.pro.entity.Ecity;
import com.cgq.pro.entity.User;
import com.cgq.pro.service.GeneratorService;
import com.cgq.utils.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Component
public class BaseAction extends ActionSupport{

	private static final long serialVersionUID = 2989524125862180864L;
	
	protected JSONObject resultObj ;
	protected String rows;//每页显示的记录数  
	protected String page;//当前第几页
	/**权限编号*/
	protected int permissionId;
	/**当前城市*/
	protected Ecity ecity;

	/**编号生成服务*/
	@Autowired
	protected GeneratorService service;
    
	public JSONObject getResultObj() {
		return resultObj;
	}
	public void setResultObj(JSONObject resultObj) {
		this.resultObj = resultObj;
	}
	public String getRows() {
		return rows;
	}
	public void setRows(String rows) {
		this.rows = rows;
	}
	public String getPage() {
		return page;
	}
	public void setPage(String page) {
		this.page = page;
	}
	public int getPermissionId() {
		return permissionId;
	}
	public void setPermissionId(int permissionId) {
		this.permissionId = permissionId;
	}
	public Ecity getEcity() {
		return ecity;
	}
	public void setEcity(Ecity ecity) {
		this.ecity = ecity;
	}
	/**
	 * 失败
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneError(){
		ajaxDoneMessage( AjaxResult.error().toString());
	}
	/**
	 * 失败并返回消息
	 * @param message 消息主体
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneError(String message){
		ajaxDoneMessage( AjaxResult.error(message).toString());
	}
	/**
	 * 失败并返回消息
	 * @param message 消息主体
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneError(String title,Exception e){
		String message=e!=null?e.getMessage():title;
		if(e instanceof Exception){
			Exception i18nE=(Exception)e;
			if(i18nE.getMessage()!=null){
				message=this.getText(i18nE.getMessage());
			}
		}
		ajaxDoneMessage( AjaxResult.error(title,message).toString());
	}
	/**
	 * 成功
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneSuccess(){
		ajaxDoneMessage( AjaxResult.success().toString());
	}
	/**
	 * 成功并返回消息
	 * @param message 消息主体
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneSuccess(String message){
		ajaxDoneMessage( AjaxResult.success(message).toString());
	}
	/**
	 * 成功并返回消息
	 * @param message 消息主体
	 * @return
	 * @throws Exception 
	 */
	protected void ajaxDoneSuccess(String title,String message){
		ajaxDoneMessage( AjaxResult.success(title,message).toString());
	}
	
	protected void ajaxDoneSuccess(Object data){
		ajaxDoneMessage(AjaxResult.success(data).toString());
	}
	
	private void ajaxDoneMessage(String message){
		try {
			ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
			ServletActionContext.getResponse().getWriter().write(message);
			ServletActionContext.getResponse().getWriter().flush();
		} catch (Exception e) {
			System.out.println("返回信息错误："+e.toString());
		}
		
	}
	
	/**
	 * 获取当前登录用户
	 * @return
	 */
	protected User getCurrentUser(){
		return (User) ActionContext.getContext().getSession().get(Constants.USER_SESSION);
	}
	
	/**
	 * 取绝对路径
	 * @return
	 */
	protected String getRootPath(){
		HttpServletRequest request=ServletActionContext.getRequest();
		return request.getSession().getServletContext().getRealPath("/");
	}
}
